package com.part2.action.common.form;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ValueObject implements java.io.Serializable {
	private static Log log = LogFactory.getLog(ValueObject.class);
	/**
	 * A Hashmap that store info fields(including guid, templateid, info
	 * elements, etc.)
	 */
	private HashMap mFields;
	private HashMap mXMLString;
	// private static Document doc ;

	public ValueObject() {
		mFields = new HashMap();
		mXMLString = new HashMap();
	}

	public ValueObject(HashMap pFields) {
		setFields(pFields);
	}

	public HashMap getFields() {
		return mFields;
	}

	public void setFields(HashMap pFields) {
		mFields = pFields;
	}

	public void addFields(HashMap pFields) {
		mFields.putAll(pFields);
	}

	/**
	 * Gets info value object field name
	 * 
	 * @param pName
	 * @return
	 */
	public String getField(String pName) {
		String fieldValue = (String) mFields.get(pName);
		return fieldValue == null ? "" : fieldValue.trim();
	}

	public void setField(String pName, String pValue) {
		mFields.put(pName, pValue);
	}

	public void setXMLField(String pName, String pValue) {
		// if(pName==null)
		// return;
		// int i= pName.indexOf(":");
		// String xmlName = pName.substring(0,i-1);
		// String fieldName = pName.substring(i);
		// XmlDocument doc = new XmlDocument();
		// XmlDocument xDoc=getXmlDocument(xmlName);
		// xDoc.createXmlDocument(fieldName,pValue);
		mXMLString.put(pName, pValue);
	}

	public String getXMLField(String pName) {
		if (mXMLString.isEmpty())
			ParseXML(pName);
		if (mXMLString.isEmpty())
			return null;
		// ParseXML(pName);
		String fieldValue = (String) mXMLString.get(pName);
		return fieldValue == null ? "" : fieldValue;
	}

	public List getXMLFieldName(String xName) {
		if (mXMLString.isEmpty())
			ParseXML(xName);
		if (mXMLString.isEmpty())
			return null;
		// ParseXML(pName);
		return (List) mXMLString.get(xName);
	}

	public void ParseXML(String pName) {
		List list = new ArrayList();
		if (pName == null)
			return;
		int i = pName.indexOf(":");
		String xName = "";
		if (i > 0) {
			xName = pName.substring(0, i);
		} else {
			xName = pName;
		}
		// String fieldName = pName.substring(i);
		SAXReader reader = new SAXReader();
		// String values=null;
		log.info("----ParseXML---");
		if (getField(xName) == null || getField(xName).equals(""))
			return;
		try {
			log.info("----ParseXML---" + xName + "++++" + getField(xName));
			Document doc = (Document) reader.read(new StringReader(
					getField(xName)));
			Element contentEl = doc.getRootElement();
			Iterator itr = contentEl.elements().iterator();
			while (itr.hasNext()) {
				Element tempEl = (Element) itr.next();
				list.add(tempEl.getName());
				log.info("xName:" + tempEl.getName() + "xvalue:"
						+ tempEl.getText());
				// values=tempEl.getText();
				setXMLField(xName + ":" + tempEl.getName(), tempEl.getText());
			}
			mXMLString.put(pName, list);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getXML(String xmlName) {
		if (mXMLString.isEmpty())
			return null;
		// DocumentFactory docFactory = DocumentFactory.getInstance();
		// Element xdoc=(Element) docFactory.createElement(xmlName);
		log.info("----getxml---");
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\"?>");
		sb.append("<" + xmlName + ">\n");
		Iterator keys = mXMLString.keySet().iterator();
		while (keys.hasNext()) {
			String pName = keys.next().toString();
			String values = (String) mXMLString.get(pName);
			if (pName == null || pName.indexOf(xmlName) < 0)
				continue;
			int i = pName.indexOf(":");
			// String xName = pName.substring(0,i-1);
			String fieldName = pName.substring(i + 1);
			sb.append("<" + fieldName + ">\n");
			sb.append(values);
			sb.append("</" + fieldName + ">\n");
			// Element valueEl = (Element) docFactory.createElement(fieldName);
			// ((Element) valueEl).addText(values);
			// xdoc.add(valueEl);
		}
		sb.append("</" + xmlName + ">");
		String fieldValue = (String) sb.toString();
		/*
		 * try{ fieldValue = new String(fieldValue.getBytes(), "UTF-8");
		 * }catch(UnsupportedEncodingException e){ e.printStackTrace(); }
		 */
		log.info("----xmlfieldValue---" + fieldValue);
		return fieldValue == null ? "" : fieldValue;
	}

	public String toString() {
		StringBuffer ret = new StringBuffer();
		ret.append("\n------------Value Object----------");
		Iterator keys = mFields.keySet().iterator();
		while (keys.hasNext()) {
			String key = (String) keys.next();
			ret.append("\n" + key + ":" + getField(key));
		}
		keys = mXMLString.keySet().iterator();
		while (keys.hasNext()) {
			String key = (String) keys.next();
			ret.append("\n" + key + ":" + mXMLString.get(key));
		}
		return ret.toString();
	}
}